package com.shxc.smarttrade.kvb.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.shxc.smarttrade.R;
import com.shxc.smarttrade.base.BaseActivity;
import com.shxc.smarttrade.info.Contents;
import com.shxc.smarttrade.kvb.fragment.KvbOpen1Fragment;
import com.shxc.smarttrade.kvb.fragment.KvbOpen2Fragment;
import com.shxc.smarttrade.kvb.fragment.KvbOpen3Fragment;
import com.shxc.smarttrade.kvb.fragment.KvbOpen4Fragment;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.xchris.view.annotation.ContentView;
import org.xchris.view.annotation.Event;
import org.xchris.view.annotation.ViewInject;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * Kvb开户
 */
@ContentView(R.layout.activity_kvb)
public class KvbOpenAccountActivity extends BaseActivity {

    @ViewInject(R.id.title_textview)
    TextView title_textview;
    @ViewInject(R.id.left_imageview)
    ImageView left_imageview;
    @ViewInject(R.id.point1)
    View point1;
    @ViewInject(R.id.point2)
    View point2;
    @ViewInject(R.id.point3)
    View point3;
    @ViewInject(R.id.point4)
    View point4;
    @ViewInject(R.id.line1)
    View line1;
    @ViewInject(R.id.line2)
    View line2;
    @ViewInject(R.id.line3)
    View line3;
    @ViewInject(R.id.kvb_title_textview)
    TextView kvb_title_textview;
    @ViewInject(R.id.framelayout)
    FrameLayout framelayout;

    private int progress = 1;//注册进度

    private FragmentTransaction mFragmentTransaction;//fragment事务
    private FragmentManager mFragmentManager;//fragment管理者

    private KvbOpen1Fragment fragment1;
    private KvbOpen2Fragment fragment2;
    private KvbOpen3Fragment fragment3;
    private KvbOpen4Fragment fragment4;

    private InputMethodManager imm;
    String account;
    String password;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        imm = (InputMethodManager) this
                .getSystemService(Context.INPUT_METHOD_SERVICE);

        title_textview.setText("KVB开立账户");
        left_imageview.setVisibility(View.VISIBLE);

        mFragmentManager = getSupportFragmentManager();//获取到fragment的管理对象

        listenerProgress();
    }

    private void listenerProgress() {

        point1.setEnabled(true);
        point2.setEnabled(false);
        point2.setBackgroundResource(R.drawable.bg_round_kvb_gray);
        point3.setEnabled(false);
        point3.setBackgroundResource(R.drawable.bg_round_kvb_gray);
        point4.setEnabled(false);
        point4.setBackgroundResource(R.drawable.bg_round_kvb_gray);
        line1.setBackgroundColor(resource.getColor(R.color.dividing_line_color));
        line2.setBackgroundColor(resource.getColor(R.color.dividing_line_color));
        line3.setBackgroundColor(resource.getColor(R.color.dividing_line_color));

        switch (progress) {
            case 1:
                kvb_title_textview.setText("一.填写注册资料");
                break;
            case 2:
                kvb_title_textview.setText("二.问卷调查");
                point2.setEnabled(true);
                point2.setBackgroundResource(R.drawable.bg_round_kvb);
                line1.setBackgroundColor(resource.getColor(R.color.kvb_account));
                break;
            case 3:
                kvb_title_textview.setText("三.签订协议");
                point2.setEnabled(true);
                point2.setBackgroundResource(R.drawable.bg_round_kvb);
                line1.setBackgroundColor(resource.getColor(R.color.kvb_account));
                point3.setEnabled(true);
                point3.setBackgroundResource(R.drawable.bg_round_kvb);
                line2.setBackgroundColor(resource.getColor(R.color.kvb_account));
                break;
            case 4:
                kvb_title_textview.setText("四.开户完成");
                point1.setEnabled(false);
                point2.setBackgroundResource(R.drawable.bg_round_kvb);
                line1.setBackgroundColor(resource.getColor(R.color.kvb_account));
                point3.setBackgroundResource(R.drawable.bg_round_kvb);
                line2.setBackgroundColor(resource.getColor(R.color.kvb_account));
                point4.setBackgroundResource(R.drawable.bg_round_kvb);
                line3.setBackgroundColor(resource.getColor(R.color.kvb_account));
                break;
        }

        setClick();
    }

    @Event({R.id.left_imageview, R.id.point1, R.id.point2, R.id.point3})
    private void onClick(View view) {
        switch (view.getId()) {
            case R.id.left_imageview:
                //关闭
                finish();
                break;
            case R.id.point1:
                //第一阶段
                progress = 1;
                listenerProgress();
                break;
            case R.id.point2:
                //第二阶段
                progress = 2;
                listenerProgress();
                break;
            case R.id.point3:
                //第三阶段
                progress = 3;
                listenerProgress();
                break;
        }
    }

    Fragment fragment;

    public void setClick() {
        //开启事务
        mFragmentTransaction = mFragmentManager.beginTransaction();
        //显示之前将所有的fragment都隐藏起来,在去显示我们想要显示的fragment
        hideFragment(mFragmentTransaction);
        switch (progress) {
            case 1:
                if (fragment1 == null) {
                    fragment1 = new KvbOpen1Fragment();
                    mFragmentTransaction.add(R.id.framelayout, fragment1);
                } else {
                    mFragmentTransaction.show(fragment1);
                }
                fragment = fragment1;
                break;
            case 2:
                if (fragment2 == null) {
                    fragment2 = new KvbOpen2Fragment();
                    mFragmentTransaction.add(R.id.framelayout, fragment2);
                } else {
                    mFragmentTransaction.show(fragment2);
                }
                fragment = fragment2;
                break;
            case 3:
                if (fragment3 == null) {
                    fragment3 = new KvbOpen3Fragment();
                    mFragmentTransaction.add(R.id.framelayout, fragment3);
                } else {
                    mFragmentTransaction.show(fragment3);
                }
                fragment = fragment3;
                break;
            case 4:
                if (fragment4 == null) {
                    fragment4 = KvbOpen4Fragment.newInstance(account, password);
                    mFragmentTransaction.add(R.id.framelayout, fragment4);
                } else {
                    mFragmentTransaction.show(fragment4);
                }
                fragment = fragment4;
                break;
        }
        //提交事务
        mFragmentTransaction.commit();
    }

    /**
     * 用来隐藏fragment的方法
     *
     * @param fragmentTransaction
     */
    private void hideFragment(FragmentTransaction fragmentTransaction) {
        //如果此fragment不为空的话就隐藏起来
        if (fragment1 != null) {
            fragmentTransaction.hide(fragment1);
        }
        if (fragment2 != null) {
            fragmentTransaction.hide(fragment2);
        }
        if (fragment3 != null) {
            fragmentTransaction.hide(fragment3);
        }
        if (fragment4 != null) {
            fragmentTransaction.hide(fragment4);
        }
    }

    public void makeProgress(int p) {
        progress = p;
        listenerProgress();
    }

    public void openKvbAccount() {
        fragment1.openKvbAccount();
    }

    public void openSuccess(String account, String password) {
        this.account = account;
        this.password = password;
        progress = 4;
        listenerProgress();
    }

    // 点击EditText以外的任何区域隐藏键盘
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {

        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            View v = getCurrentFocus();
            if (v != null) {
                if (progress ==1) {
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                }
            }
        }
        return super.dispatchTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            View v = getCurrentFocus();
            if (progress ==1) {
                imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
            }
        }
        return super.onTouchEvent(event);
    }
    /**
     * 解决了Fragment重叠的问题
     *
     * @param outState
     */
    @Override
    public void onSaveInstanceState(Bundle outState) {
//        super.onSaveInstanceState(outState);
        super.onSaveInstanceState(outState);
    }

}
